﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Schedule;

using VISS.API.Users;
using NCT.Mail;
using VISS.MailImpl;
using NCT.Framework;


namespace MessageNotificationService {
    public partial class frmMain : Form {

        private ScheduleTimer _Timer = new ScheduleTimer();

        private DateTime SelectedTime = new DateTime(2010, 11, 1, 23, 59, 0);
        private TimeSpan ExecuteTime = new TimeSpan(0, 23, 59, 0, 0);

        public frmMain() {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e) {
            lb_CurrentTime.Text = SelectedTime.ToLongTimeString();
            _Timer.AddJob(
                new Schedule.ScheduledTime(EventTimeBase.Daily, ExecuteTime),
                new TickHandler(SendMail)
            );
        }

        delegate void TickHandler();
        private void SendMail() {
            File.AppendAllText("log.txt", "------------------------" + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " Begin Send Mail To User--------------------------------------\r\n");

            DateTime currentTime = DateTime.Now;
            DateTime startTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0);
            DateTime endTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59);

            List<UserMessageNotificationEnt> userList = UserMessageNotificationBC.GetAll(startTime, endTime);

            for (int i = 0; i < userList.Count; i++) {
                MailEnt.MailMessagesNotification ent = new MailEnt.MailMessagesNotification();
                ent.Subject = "VISS - Thông báo từ VISS: Tin nhắn mới trong ngày";
                ent.Email = userList[i].Email;
                ent.FullName = userList[i].Name;
                ent.UserKey = userList[i].UserKey;
                ent.MessagesInDay = userList[i].MessagesInDay;
                MailEnt.MailInfo mailInfo = new MailEnt.MailInfo();
                mailInfo.MailContent = SerializerUtils.JsonSerialize(ent);
                mailInfo.SendMailType = ESendMailType.MessagesNotification;
                try {
                    if (NCT.Mail.MailQueuePD.CreateInstance().AddToQueue("frontend.viss.vn", SerializerUtils.JsonSerialize(mailInfo)) <= 0) {
                        File.AppendAllText("log.txt", "Fail add to queue for user: " + userList[i].UserKey);
                    }
                }
                catch (Exception ex) {
                    File.AppendAllText("log.txt", "Fail add to queue for user: " + userList[i].UserKey + ", " + ex.ToString());
                }
            }

            File.AppendAllText("log.txt", "----------------------------End Send Mail To User---------------------------\r\n");
        }

        private void btn_Start_Click(object sender, EventArgs e) {
            _Timer.Start();
            btn_Start.Enabled = false;
            btn_Stop.Enabled = true;
            btn_OK.Enabled = false;
            dateTime_ExcuteTime.Enabled = false;
        }

        private void btn_Stop_Click(object sender, EventArgs e) {
            _Timer.Stop();
            btn_Start.Enabled = true;
            btn_Stop.Enabled = false;
            btn_OK.Enabled = true;
            dateTime_ExcuteTime.Enabled = true;
        }

        private void btn_OK_Click(object sender, EventArgs e) {
            SelectedTime = dateTime_ExcuteTime.Value;
            lb_CurrentTime.Text = SelectedTime.ToLongTimeString();
            ExecuteTime = new TimeSpan(0, SelectedTime.Hour, SelectedTime.Minute, SelectedTime.Second, 0);
            _Timer.ClearJobs();
            _Timer.AddJob(
                new Schedule.ScheduledTime(EventTimeBase.Daily, ExecuteTime),
                new TickHandler(SendMail)
            );
        }

        private void frmMain_Resize(object sender, EventArgs e) {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void notifyIcon_SamllIcon_DoubleClick(object sender, EventArgs e) {
            Show();
            WindowState = FormWindowState.Normal;
        }
    }
}
